package main

import (
	"crypto/sha256"
	"fmt"
	"github.com/anaskhan96/go-password-encoder"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"mxshop_srvs/user_srv/model"
	"mxshop_srvs/user_srv/proto"
)

var userClient proto.UserClient
var conn *grpc.ClientConn

func Init() {
	var err error
	conn, err = grpc.Dial("localhost:50051", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	userClient = proto.NewUserClient(conn)
}

func TestGetUserList() {
	list, err := userClient.GetUserList(context.Background(), &proto.PageInfo{Pn: 1, PSize: 4})
	if err != nil {
		panic(err)
	}
	for _, user := range list.Data {
		fmt.Println(user.Mobile, user.NickName, user.PassWord)
		checkRsp, err := userClient.CheckPassword(context.Background(), &proto.PasswordCheckInfo{Password: "admin123", EncryptedPassword: user.PassWord})
		if err != nil {
			panic(err)
		}
		fmt.Println(checkRsp.Success)
	}
}
func TestCreateUser() {
	options := &password.Options{16, 100, 32, sha256.New}
	salt, encodedPwd := password.Encode("admin123", options)
	newPassword := fmt.Sprintf("$pbkdf2-sha256$%s$%s", salt, encodedPwd)
	for i := 0; i < 10; i++ {
		user := model.User{
			Nickname: fmt.Sprintf("user_%d", i),
			Mobile:   fmt.Sprintf("1382222222%d", i),
			Password: newPassword,
		}
		rsp, err := userClient.CreateUser(context.Background(), &proto.CreateUserInfo{
			NickName: user.Nickname,
			Password: user.Password,
			Mobile:   user.Mobile,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(rsp.Id)
	}
}

func main() {
	Init()
	//TestCreateUser()
	TestGetUserList()
	conn.Close()
}
